#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) {
        int len = tasks.size();
        vector<int> V(26);
        for (char c : tasks) ++V[c - 'A'];
        sort(V.begin(), V.end(), [](int& x, int& y) {return x > y; });
        int cnt = 1;
        while (cnt < V.size() && V[cnt] == V[0]) {
            ++cnt;
        }
        return max(len, cnt + (V[0] - 1) * (n + 1));
        
    }
};